package com.lbd99.data.lbd.controller;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lbd99.data.lbd.dao.TUserMapper;
import com.lbd99.data.lbd.service.TUserService;
import com.lbd99.data.lbd.vo.SysAccount;
import com.lbd99.data.lbd.vo.TConfig;
import com.lbd99.data.lbd.vo.TUser;

@Controller
@RequestMapping("user")
public class TUserController {

    @Autowired
    private TUserService tUserService;

    @RequestMapping(value = "/getUserByPhone")
    @ResponseBody
    public Map<String, Object> getUserByPhone(String phone){
    	TUser user = tUserService.selectByPhone(phone);
    	Map<String,Object> result=new HashMap<String,Object>();
    	result.put("isSuc",true);
    	result.put("user", user);
    	return result;
    }
    /**
     * 分页查询数据
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(value = "/getUser")
    @ResponseBody
    public Map<String, Object> getUser(int page,int rows,HttpServletRequest request,HttpSession httpSession) {
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String,Object> map2=new HashMap<String,Object>();
        map2.put("phone",request.getParameter("phone"));
        map2.put("uid",StringUtils.trim(request.getParameter("uid")));
        /*====================================*/
        SysAccount sysAccount = (SysAccount)httpSession.getAttribute("SESSION_INFO_OBJECT");//获取当前登录的用户
        if(sysAccount.getType()==1){//如果是普通管理员，用手机号查询出user信息
        	TUser user = tUserService.selectByPhone(sysAccount.getTel());
        	if(user!=null){
        		map2.put("rootUserId",user.getId()+"");
        	}
        }
        /*====================================*/
        //  List<TUser> list = tUserService.selectList(map2);
        PageHelper.startPage(page, rows);
        List<Map<String,Object>>  list=tUserService.selectListMap(map2);
        PageInfo<Map<String,Object>> pages = new PageInfo<Map<String,Object>>(list);
        result.put("total", pages.getTotal());
        result.put("rows", pages.getList());
        return result;
    }
    @RequestMapping(value = "/getChildUserByParentId")
    @ResponseBody
    public Map<String, Object> getChildUserByParentId(int page,int rows,Integer p_id) {
    	 Map<String, Object> result = new HashMap<String, Object>();
    	 /*========================================================*/
    	 Map<String,Object> datamap=new HashMap<String,Object>();
    	 datamap.put("p_id", p_id);
    	 /*========================================================*/
    	 PageHelper.startPage(page, rows);
    	 List<Map<String,Object>>  list=tUserService.getChildUserByParentId(datamap);
    	 PageInfo<Map<String,Object>> pages = new PageInfo<Map<String,Object>>(list);
         result.put("total", pages.getTotal());
         result.put("rows", pages.getList());
         return result;
    }

    //更新钱包
    @RequestMapping(value = "/updateWallet")
    @ResponseBody
    public Map<String, Object> updateWallet(Integer userId,BigDecimal amount,String tradePwd,HttpSession httpSession) {
    	Map<String, Object> map = new HashMap<String, Object>();
    	SysAccount sysAccount = (SysAccount) httpSession.getAttribute("SESSION_INFO_OBJECT");
		if(sysAccount == null){
			map.put("isSuc", false);
			map.put("msg", "请先登录!");
			return map;
		}
        return tUserService.updateWallet(userId,amount,tradePwd,sysAccount.getId());
    }
    
    /**
     * 转账限制和买卖限制
     */
    @RequestMapping(value = "/updateConfinedStatus")
    @ResponseBody
    public Map<String,Object> updateConfinedStatus(int id,String field,int status){
		return tUserService.updateConfinedStatus(id,field,status);
    }
    
    /**
     * 分页查询系统参数
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(value = "/getParameter")
    @ResponseBody
    public Map<String, Object> getParameter(int page,int rows,HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        PageHelper.startPage(page, rows);
        List<Map<String,Object>>  list=tUserService.getParameter();
        PageInfo<Map<String,Object>> pages = new PageInfo<Map<String,Object>>(list);
        result.put("total", pages.getTotal());
        result.put("rows", pages.getList());
        return result;
    }
    
    
    /**
     * 更新系统参数
     * @param tConfig
     * @return
     */
    @RequestMapping(value = "/updateConfig")
    @ResponseBody
    public Map<String, Object> updateConfig(TConfig tConfig) {
        return tUserService.updateConfig(tConfig);
    }
    
    /**
     * 添加系统参数
     * @param tConfig
     * @return
     */
    @RequestMapping(value = "/insertConfig")
    @ResponseBody
    public Map<String, Object> insertConfig(TConfig tConfig) {
        return tUserService.insertConfig(tConfig);
    }
    
    /**
     * 获取兑换记录
     * @param tConfig
     * @return
     */
    @RequestMapping(value = "/getExchangeList")
    @ResponseBody
    public Map<String, Object> getExchangeList(int page,int rows,String phone,String startTime,String endTime,HttpSession httpSession) {
    	SysAccount sysAccount = (SysAccount)httpSession.getAttribute("SESSION_INFO_OBJECT");//获取当前登录的用户
    	Map<String, Object> result = new HashMap<String, Object>();
    	Map<String,Object> map=new HashMap<String,Object>();
        if(sysAccount.getType()==1){//如果是普通管理员，用手机号查询出user信息
        	TUser user = tUserService.selectByPhone(sysAccount.getTel());
        	if(user!=null){
        		map.put("rootUserId",user.getId()+"");
        	}
        }
		 //拼装map条件
		 map.put("phone",phone);
		 if(StringUtils.isNotBlank(startTime)){
			map.put("startTime",startTime+" 00:00:00");
		 }
		 if(StringUtils.isNotBlank(endTime)){
			 map.put("endTime",endTime+" 23:59:59");
		 }
		 PageHelper.startPage(page, rows);
		 
		 List<Map<String,Object>> list = tUserService.getExchangeList(map);
		 PageInfo<Map<String,Object>> pages = new PageInfo<Map<String,Object>>(list);
		 result.put("total", pages.getTotal());
		 result.put("rows", pages.getList());
		return result;
    }
    	
    /**
     * 本周兑换明细
     */
    @RequestMapping(value = "/getExchangeListWeek")
    @ResponseBody
    public Map<String, Object> getExchangeListWeek(int page,int rows,HttpSession httpSession){
    	//拼装map条件
		 Map<String,Object> map=new HashMap<String,Object>();
		 /*===================================================================================================*/
	     SysAccount sysAccount = (SysAccount)httpSession.getAttribute("SESSION_INFO_OBJECT");//获取当前登录的用户
	        if(sysAccount.getType()==1){//如果是普通管理员，用手机号查询出user信息
	        	TUser user = tUserService.selectByPhone(sysAccount.getTel());
	        	if(user!=null){
	        		//根据用户id查出该用户伞下的ID集合(包含自身)
	        		List<Integer> ids = tUserService.selectTreeIds(user.getId());
	        		if(ids!=null && ids.size()>0){
	        			map.put("ids",ids);
	        		}
	        	}
	        }
		 /*======================================================================================================*/
    	Map<String, Object> result = new HashMap<String, Object>();

    	PageHelper.startPage(page, rows);
    	List<Map<String,Object>> list = tUserService.getExchangeListWeek(map);
    	PageInfo<Map<String,Object>> pages = new PageInfo<Map<String,Object>>(list); 
    	result.put("total", pages.getTotal());
		result.put("rows", pages.getList());
		return result;
    }
    
    /**
     * 查询充值记录
     * @param page
     * @param rows
     * @param phone
     * @param nickName
     * @return
     */
    @RequestMapping(value = "/getRechargeRecord")
    @ResponseBody
    public Map<String, Object> getRechargeRecord(int page,int rows,String phone,String nickName,HttpSession httpSession){
    	SysAccount sysAccount = (SysAccount)httpSession.getAttribute("SESSION_INFO_OBJECT");//获取当前登录的用户
    	String rootUserId = null;
        if(sysAccount.getType()==1){//如果是普通管理员，用手机号查询出user信息
        	TUser user = tUserService.selectByPhone(sysAccount.getTel());
        	if(user!=null){
        		rootUserId = user.getId()+"";
        	}
        }
    	return tUserService.getRechargeRecord(page, rows, phone, nickName,rootUserId);
    }
    
    
    @RequestMapping(value = "/getUserRevise")
    @ResponseBody
    public Map<String, Object> getUserRevise(String phone,BigDecimal ifiv,BigDecimal ifip,int id){
    	//修改手机号码
    	if(!StringUtils.isEmpty(phone)){
    		return tUserService.updateUserPhone(phone,id);
    	}else{
    		return tUserService.rechargeUserIfipb(ifiv,ifip,id);
    	}
    }
    
    @RequestMapping(value = "/confineTradeCountSave")
    @ResponseBody
    public Map<String, Object> confineTradeCountSave(Integer id,Integer count_trade){
		return tUserService.confineTradeCountSave(id,count_trade);
    }

    @RequestMapping(value = "/updatePwd")
    @ResponseBody
    public Map<String, Object> updatePwd(Integer id,String pwd,String trade_pwd){
        return tUserService.updatePwd(id,pwd,trade_pwd);
    }


    
}
